package com.snail.readyvr.activity;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.vr.sdk.widgets.pano.VrPanoramaEventListener;
import com.google.vr.sdk.widgets.pano.VrPanoramaView;
import com.snail.readyvr.R;
import com.snail.readyvr.glide.BlurTransformation;
import com.snail.readyvr.util.LogUtils;
import com.snail.readyvr.util.Utils;

import butterknife.BindView;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by MRKING on 2016/11/23.
 * 背景为VR图片的基类
 */

public abstract class BaseVRImageActivity extends CheckPermissionsActivity {


    @Nullable
    @BindView(R.id.pano_view)
    VrPanoramaView mVrPanoramaView;//id必须为pano_view

    private VrPanoramaView.Options mPanoOptions = new VrPanoramaView.Options();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        enableSystemBarTint(false, 0, 0);
        super.onCreate(savedInstanceState);
        StatusBarCompat.translucentStatusBar(this, true);
        if (mVrPanoramaView != null)
            loadPanoramaImg();
    }


    private void loadPanoramaImg() {
        mPanoOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
        mVrPanoramaView.setEventListener(new ActivityEventListener());
        mVrPanoramaView.setStereoModeButtonEnabled(false);
        mVrPanoramaView.setFullscreenButtonEnabled(false);
        mVrPanoramaView.setInfoButtonEnabled(false);
        mVrPanoramaView.setTouchTrackingEnabled(false);
        Glide.with(this).load(Uri.parse(Utils.VR_IMG_URL)).asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transform(new BlurTransformation(this, true))
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        mVrPanoramaView.loadImageFromBitmap(resource, mPanoOptions);
                    }
                });
    }


    /**
     * Listen to the important events from widget.
     */
    private class ActivityEventListener extends VrPanoramaEventListener {
        /**
         * Called by pano widget on the UI thread when it's done loading the image.
         */
        @Override
        public void onLoadSuccess() {

        }

        /**
         * Called by pano widget on the UI thread on any asynchronous error.
         */
        @Override
        public void onLoadError(String errorMessage) {
            LogUtils.e(TAG, "Error loading pano: " + errorMessage);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mVrPanoramaView != null)
            mVrPanoramaView.pauseRendering();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mVrPanoramaView != null)
            mVrPanoramaView.resumeRendering();
    }

    @Override
    protected void onDestroy() {
        // Destroy the widget and free memory.
        //mVrPanoramaView.pauseRendering();
        if (mVrPanoramaView != null) {
            mVrPanoramaView.pauseRendering();
            mVrPanoramaView.shutdown();
        }
        super.onDestroy();
    }


}
